//
//  WQRoomMainViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/18.
//

#import "WQRoomMainViewController.h"
#import "WQDeviceRootListViewController.h"
#import <ThingSmartBizCore/ThingSmartBizCore.h>

@interface WQRoomMainViewController () <JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) UIImageView           *backColorImageView;

@property (nonatomic, strong) UIButton           *logoButton;
@property (nonatomic, strong) UIButton           *framilyButton; // 家庭

@property (nonatomic, strong) UIButton           *rightMicrButton; // 麦克风
@property (nonatomic, strong) UIButton           *rightAddButton; // +

@property (nonatomic, strong) JXCategoryTitleView *jk_categoryView; // 分页
@property (nonatomic, strong) JXCategoryIndicatorLineView *indicator;
@property (nonatomic, strong) JXCategoryListContainerView *jk_listContainerView;

@property (nonatomic, strong) UIButton           *moreButton;

@end

@implementation WQRoomMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.hbd_barTintColor = UIColor.clearColor;
    self.hbd_barAlpha = 0;
    
    self.navigationItem.leftBarButtonItems = @[
        [[UIBarButtonItem alloc] initWithCustomView:self.logoButton],
        [[UIBarButtonItem alloc] initWithCustomView:self.framilyButton]
    ];
    
    self.navigationItem.rightBarButtonItems = @[
        [[UIBarButtonItem alloc] initWithCustomView:self.rightAddButton],
//        [[UIBarButtonItem alloc] initWithCustomView:self.rightMicrButton]
    ];
    
    [self.view addSubviews:[NSArray arrayWithObjects:self.backColorImageView, nil]];
    [self.view insertSubview:self.backColorImageView atIndex:0];
    [self.backColorImageView addSubviews:[NSArray arrayWithObjects:self.jk_categoryView, self.jk_listContainerView, self.moreButton, nil]];
    [self fragmentUI_ViewSet];
    
    [self reloadRoomMainVcMethods];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadRoomMainVcMethods) name:kNotificationFamilyUpdate object:nil];
    
    
    
    
    
//    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHomeDataProtocol) withInstance:self];
//    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHouseIndexProtocol) withInstance:self];
}

// 实现对应的协议方法
//- (ThingSmartHome *)getCurrentHome {
//    ThingSmartHome *home = [ThingSmartHome homeWithHomeId:WQShare.userHomeModel.homeId];
//    return home;
//}
//- (BOOL)homeAdminValidation {
//    return YES;
//}

- (void)reloadRoomMainVcMethods {
    
    if (WQShare.isLogin == NO) {
        [self.framilyButton setTitle:@"" forState:0];
        self.jk_categoryView.titles = @[];
        [self.jk_categoryView reloadData];
        return;
    }
        
    [self.framilyButton setTitle:[NSString stringWithFormat:@"  %@", WQShare.userHomeModel.homeModel.name] forState:0];
    
    NSArray *roomNameArray = [WQShare.userHomeModel.roomList kj_mapArray:^NSString * _Nonnull(ThingSmartRoomModel *  _Nonnull object) {
        return object.name;
    }];
    
    self.jk_categoryView.titles = roomNameArray;
    [self.jk_categoryView reloadData];
}

#pragma mark ------- Click Methods -------

- (void)rightAddButtonClick {
    [TSHandleAlertView pad_showSystemSheetViewWithTitleArray:@[@"添加设备", @"创建场景", @"扫一扫"] confirm:^(NSInteger buttonTag) {
        
        switch (buttonTag) {
            case 0:
                [WQShare addDeviceMethods];
                break;
            case 1:
                [WQShare addSceneMainMethods];
                break;
            case 2:
                [WQShare openScanMainMethods];
                break;
                
            default:
                break;
        }
        
    } view:[UIApplication currentRootTopViewController].view frame:CGRectZero];
}

#pragma mark ------- JXCategoryListContainerViewDelegate -------

//返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.jk_categoryView.titles.count;
}

//根据下标index返回对应遵从`JXCategoryListContentViewDelegate`协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    ThingSmartRoomModel *roomModel = WQShare.userHomeModel.roomList[index];
    
    WQDeviceRootListViewController *vc = [[WQDeviceRootListViewController alloc] init];
    vc.roomModel = roomModel;
    
    return vc;
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.backColorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.jk_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backColorImageView);
        make.right.equalTo(self.backColorImageView.mas_right).offset(-kFragmemtFloat(50));
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.mas_equalTo(kFragmemtFloat(30));
    }];
    [self.jk_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.backColorImageView);
        make.top.equalTo(self.jk_categoryView.mas_bottom).offset(kFragmemtFloat(10));
    }];
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.jk_categoryView);
        make.right.equalTo(self.backColorImageView.mas_right).offset(-kFragmemtFloat(11));
        make.size.mas_equalTo(kSizzleKapadate(33, 33));
    }];

}

- (UIImageView *)backColorImageView {
    if (!_backColorImageView) {
        _backColorImageView = [[UIImageView alloc] initWithImage:kImageName(@"room_colorbg_image")];
        _backColorImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backColorImageView.userInteractionEnabled = YES;
    }
    return _backColorImageView;
}

- (UIButton *)logoButton {
    if (!_logoButton) {
        _logoButton = [UIButton initWithImage:kImageName(@"base_nav_logo") selectedImage:nil];
    }
    return _logoButton;
}

- (UIButton *)framilyButton {
    if (!_framilyButton) {
        _framilyButton = [UIButton initWithImage:kImageName(@"base_nav_framily") selectedImage:nil];
        _framilyButton.titleLabel.font = kAdaptedMediumFontSize(15);
        [_framilyButton setTitleColor:k_Color_222222 forState:0];
        
        [_framilyButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
            [WQShare showExchangeFramilyListView];
        }];
    }
    return _framilyButton;
}

- (UIButton *)rightMicrButton {
    if (!_rightMicrButton) {
        _rightMicrButton = [UIButton initWithImage:kImageName(@"rome_nav_voice") selectedImage:nil];
    }
    return _rightMicrButton;
}

- (UIButton *)rightAddButton {
    if (!_rightAddButton) {
        _rightAddButton = [UIButton initWithImage:kImageName(@"rome_nav_add") selectedImage:nil];
        [_rightAddButton addTarget:self action:@selector(rightAddButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightAddButton;
}

- (JXCategoryTitleView *)jk_categoryView {
    if (!_jk_categoryView) {
        _jk_categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//        _jk_categoryView.titles = @[@"客厅", @"主卧", @"工作室", @"女孩房", @"男孩房"];
        _jk_categoryView.averageCellSpacingEnabled = NO;
//        _jk_categoryView.titleLabelVerticalOffset = kAdaptedFloat(5);
//        _jk_categoryView.cellSpacing = kAdaptedFloat(35);
//        _jk_categoryView.cellWidth = kAdaptedFloat(20);
        _jk_categoryView.titleColor = k_Color_707179;
        _jk_categoryView.titleSelectedColor = k_Color_333333;
        _jk_categoryView.titleFont = kAdaptedMediumFontSize(16);
//        _jk_categoryView.contentEdgeInsetLeft = kAdaptedFloat(25);
        _jk_categoryView.indicators = @[self.indicator];
    }
    return _jk_categoryView;
}

- (JXCategoryIndicatorLineView *)indicator {
    if (!_indicator) {
        _indicator = [[JXCategoryIndicatorLineView alloc] init];
        _indicator.indicatorColor = k_Color_themeColor;
        _indicator.indicatorWidth = kFragmemtFloat(23);
//        _indicator.verticalMargin = kAdaptedFloat(5);
    }
    return _indicator;
}

- (JXCategoryListContainerView *)jk_listContainerView {
    if (!_jk_listContainerView) {
        _jk_listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
        _jk_listContainerView.backgroundColor = UIColor.clearColor;
        self.jk_categoryView.listContainer = _jk_listContainerView;
    }
    return _jk_listContainerView;
}

- (UIButton *)moreButton {
    if (!_moreButton) {
        _moreButton = [UIButton initWithImage:kImageName(@"base_sanMore_icon") selectedImage:nil];
    }
    return _moreButton;
}

@end
